package SE010;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class showTaskStu
 */
@WebServlet("/showTaskStu")
public class showTaskStu extends HttpServlet{//进入某一任务后,显示班级的所有学生,点击学生查看该学生在本班的所有任务情况
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String UserID = request.getParameter("UserID");
        String InviteID = request.getParameter("InviteID");
        String taskID = request.getParameter("taskID");
        String taskNum = request.getParameter("num");//仅供显示的任务的序号
        int stuNum = 1; //仅供显示的学生的序号
        String StudentID = null;
        String StudentName = null;
        float perCompleteRate = 0;//记录学生的任务进度
        DecimalFormat df = new DecimalFormat("#0.0%");//任务进度输出格式调整
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "root";
        Connection conn = null;//用于建立与数据库的连接
        Statement sta = null;
        ResultSet rs = null;
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作 
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        String sql = "select StudentID,StudentName,CompleteRate from v_task_stu where taskID='"+taskID+"'";
        try {
            if(conn != null) {
                sta = conn.createStatement();
            }
            if(sta != null) {
                rs = sta.executeQuery(sql);
            }
            out.print("<html>");
            out.print("<head>");
            out.print("<title>学生列表</title>");
            out.print("</head>");
            out.print("<body>");
            out.print("<center>");
            while(rs != null && rs.next()) {
                StudentID = rs.getString("StudentID");
                StudentName = rs.getString("StudentName");
                perCompleteRate = Float.parseFloat(rs.getString("CompleteRate"));  
                out.print("<a href=\"showTaskContentStu?UserID="+URLEncoder.encode(UserID)+"&StudentID="+URLEncoder.encode(StudentID)+"&InviteID="+URLEncoder.encode(InviteID)+"&taskID="+URLEncoder.encode(taskID)+"&num="+taskNum+"\">"+ stuNum++ +"."+"学号:"+StudentID+"姓名:"+StudentName+"进度:"+df.format(perCompleteRate)+"</a>"+"<br>");//隐藏控件传参，便于处理
                //out.print("<a href=\"showTaskContentStu?UserID="+UserID+"&StudentID="+StudentID+"&InviteID="+InviteID+"&taskID="+taskID+"&num="+taskNum+"\">"+ stuNum++ +"."+"学号:"+StudentID+"姓名:"+StudentName+"进度:"+df.format(perCompleteRate)+"</a>"+"<br>");//隐藏控件传参，便于处理
            }
            out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showTask?UserID="+URLEncoder.encode(UserID)+"&InviteID="+URLEncoder.encode(InviteID)+"'\">");
            //out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showTask?UserID="+UserID+"&InviteID="+InviteID+"'\">");
            out.print("</center>");
            out.print("</form>");
            out.print("</body>");
            out.print("</html>");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)
                    rs.close();
                if(sta != null)
                    sta.close();
                if(conn != null)
                    conn.close();
                rs = null;
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}